//
//  WeSellerMessageController.m
//  WeGalaxy
//
//  Created by Zheng on 2017/7/24.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "WeSellerMessageController.h"
#import "WeSellerMessageHeaderInfoView.h"
#import "WeSellerMessageCell.h"
#import "WeSellerMessageModel.h"
#import "WeSellerMeesageInputView.h"

static NSString *kWeSellerMessageCell_ID = @"kWeSellerMessageCell_ID";

@interface WeSellerMessageController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) WeSellerMessageHeaderInfoView *infoView;
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) WeSellerMeesageInputView *inputBView;
@property (nonatomic, strong) NSDictionary *dataDic;
@property (nonatomic, strong) WeSellerMessageModel *model;
@property (nonatomic, strong) NSArray *msgDataArr;

@end

@implementation WeSellerMessageController

#pragma mark - tableview delegate&datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _msgDataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    WeSellerMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:kWeSellerMessageCell_ID forIndexPath:indexPath];
    WeSellerMessageCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"WeSellerMessageCell" owner:self options:nil] lastObject];
    
    WeSellerMessageModel *model = _msgDataArr[indexPath.row];
    cell.model = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return .0001f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return .0001f;
}

#pragma mark - 输入文字
- (void)didInputText:(NSString *)str {
    WeSellerMessageModel *model = [[WeSellerMessageModel alloc] initWithDataDic:@{@"chartType":@"2",
                                                                                  @"notifactionTitle":@"交易成功",
                                                                                  @"notifcationContent":@"",
                                                                                  @"timestamp":@"2017年07月24日 10:20",
                                                                                  @"userUrl":@"http://v1.qzone.cc/avatar/201409/24/19/58/5422b1ff86ed0232.jpg%21200x200.jpg",
                                                                                  @"chatContent":str}];
    NSMutableArray *arr = [NSMutableArray arrayWithArray:_msgDataArr];
    [arr addObject:model];
    _msgDataArr = [arr copy];
    [_myTableView reloadData];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_msgDataArr.count-1 inSection:0];
    [_myTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
    
}

#pragma mark - 子视图
- (void)subviewsInView {
    [self.view addSubview:self.infoView];
    
    [_infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.height.mas_equalTo(95);
    }];
    
    [self.view addSubview:self.inputBView];
    
    [_inputBView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.height.mas_equalTo(55);
    }];
    
    [self.view addSubview:self.myTableView];
    
    [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(_infoView.mas_bottom);
        make.bottom.equalTo(_inputBView.mas_top);
    }];
}


- (WeSellerMeesageInputView *)inputBView {
    if (!_inputBView) {
        _inputBView = [[WeSellerMeesageInputView alloc] init];
        __weak typeof(self) weakSelf = self;
        _inputBView.msgBlock = ^(NSString *msg) {
            [weakSelf didInputText:msg];
         };
    }
    return _inputBView;
}

- (WeSellerMessageHeaderInfoView *)infoView {
    if (!_infoView) {
        _infoView = [[WeSellerMessageHeaderInfoView alloc] init];
    }
    return _infoView;
}

- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.rowHeight = UITableViewAutomaticDimension;
        _myTableView.estimatedRowHeight = 100.f;
        _myTableView.backgroundColor = [UIColor whiteColor];
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        [_myTableView registerNib:[UINib nibWithNibName:@"WeSellerMessageCell" bundle:nil] forCellReuseIdentifier:kWeSellerMessageCell_ID];
        
    }
    return _myTableView;
}


#pragma mark - view生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"阿八";
    
    [self subviewsInView];
    
    _dataDic = @{@"goods":@{@"price":@"180",
                            @"des":@"交易前聊一聊",
                            @"pic":@"http://v1.qzone.cc/avatar/201409/24/19/58/5422b1ff86ed0232.jpg%21200x200.jpg"},
                 @"charts":@[
                         @{@"chartType":@"1",
                           @"notifactionTitle":@"交易成功",
                           @"notifcationContent":@"",
                           @"timestamp":@"2017年07月24日 10:20",
                           @"userUrl":@"http://v1.qzone.cc/avatar/201409/24/19/58/5422b1ff86ed0232.jpg%21200x200.jpg",
                           @"chatContent":@"你好,你还没有付款呢,你好,你还没有付款呢你好,你还没有付款呢你好,你还没有付款呢你好款呢你好,你还还没有付款呢,你好,你还没有付款呢你好,你还没有付款呢你好,你还没有付款呢你好款呢你好,你还还没有付款呢,你好,你还没有付款呢你好,你还没有付款呢你好,你还没有付款呢你好款呢你好,你还还没有付款呢,你好,你还没有付款呢你好,你还没有付款呢你好,你还没有付款呢你好款呢你好,你还没款呢,你好,你还没有付款呢你呢你好,你还没款呢,你好,你还没有付款呢你呢你好,你还没款呢,你好,你还没有付款呢你好,你还没有付款呢你好,你还没有付款呢你好款呢,你好,你还没有付款呢你好,你还没有付款呢你好,你还没"},
                         @{@"chartType":@"3",
                           @"notifactionTitle":@"交易成功",
                           @"notifcationContent":@"",
                           @"timestamp":@"2017年07月24日 10:20",
                           @"userUrl":@"http://v1.qzone.cc/avatar/201409/24/19/58/5422b1ff86ed0232.jpg%21200x200.jpg",
                           @"chatContent":@"你好,你还没有付款呢"},
                         @{@"chartType":@"2",
                           @"notifactionTitle":@"交易成功",
                           @"notifcationContent":@"",
                           @"timestamp":@"2017年07月24日 10:20",
                           @"userUrl":@"http://v1.qzone.cc/avatar/201409/24/19/58/5422b1ff86ed0232.jpg%21200x200.jpg",
                           @"chatContent":@"你好,你还没有付款呢"},
                         @{@"chartType":@"2",
                           @"notifactionTitle":@"交易成功",
                           @"notifcationContent":@"",
                           @"timestamp":@"2017年07月24日 10:20",
                           @"userUrl":@"http://v1.qzone.cc/avatar/201409/24/19/58/5422b1ff86ed0232.jpg%21200x200.jpg",
                           @"chatContent":@"不着急昂"},
                         @{@"chartType":@"1",
                           @"notifactionTitle":@"交易成功",
                           @"notifcationContent":@"",
                           @"timestamp":@"2017年07月24日 10:20",
                           @"userUrl":@"http://v1.qzone.cc/avatar/201409/24/19/58/5422b1ff86ed0232.jpg%21200x200.jpg",
                           @"chatContent":@"你好,你还没有付款呢,你好,你还没有付款呢你好,你还没有付款呢你好,你还没有付款呢你好款呢你好没有付款呢你好,你还没有付款呢你好,你还没"}
                         ]
                 };
    
    _model = [[WeSellerMessageModel alloc] initWithDataDic:_dataDic];
    
    
    _msgDataArr = _model.charts;


}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    _infoView.model = _model;
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_msgDataArr.count-1 inSection:0];
    
    [_myTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
}

#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
